define(['app'],function(app){return {module: function($scope, $http, eventBusService, httpService, params) {
	
	$scope.form.father_id=params.father_id;
	$scope.form.fk_unit='self';

	//学校办别
	httpService.post(app.urls.dictionary_selectByRule,{fk_rule:"c3547dff15484941b2c1d7b5ab9e65d9"},function(data){
		if("0000" === data.code){
			$scope.bbList = data.data;
		}else{
			alert(data.msg);
		}
	});
	//学校类别
	httpService.post(app.urls.dictionary_selectByRule,{fk_rule:"182c0cbc8924435ab6773779d9f2f48f"},function(data){
		if("0000" === data.code){
			$scope.lbList = data.data;
		}else{
			alert(data.msg);
		}
	});
	httpService.post(app.urls.menu_select,{"id":params.father_id},function(data){
		if("0000" === data.code){
			$scope.menu_name = data.data[0].name;
			$scope.findButton(data.data[0].id);
		}else{
			alert(data.msg);
		}
	});

	var pageFind = function () {
		httpService.postpage(app.urls.unit_selectAttach,$scope.form,function(data){
			if("0000" === data.code){
				$scope.rows = data.data;
			}else{
				alert(data.msg);
			}
		});
	}

	var getUserMinRole = function(){
		httpService.post(app.urls.role_getUserMinRole,{},function(data){
			if("0000" === data.code){
				if(data.data.length > 0){
					$scope.form.userrole = data.data[0].minrole;
				}
				pageFind();
			}else{
				alert(data.msg);
			}
		});
	}

	$scope.findButton=function(menu_id){
		httpService.post(app.urls.button_select,{"menu_id":menu_id},function(data){
			if("0000" === data.code){
				$scope.buttons = data.data;
			}else{
				alert(data.msg);
			}
		});
	}
	//查询学校
	$scope.find = function(){
		$scope.form.list='list';
		getUserMinRole();
	}
	$scope.find();
	//按钮点击事件-条件查询
	$scope.select = function(){
    	$scope.page.current = 1;
    	$scope.find();
    }
	
	$scope.clickBtn=function(event){
		var menu = {};
		if(event=='add'){
			menu.url = "modules/content/baseinfo/unit/attach/add?fk_menuid="+params.fk_menuid+ "&father_id=" + params.father_id+"&fk_unit="+params.fk_unit;
			eventBusService.publish("modules.content.load",menu);
			}
		if(event=='update'){
			var values = [];
			$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
				values.push($(this).val());
			});
			if(values.length < 1){
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择修改数据。"});
			}else if(values.length > 1){
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"您选择了多行数据，请重新选择。"});
			}else {
				menu.url = "modules/content/baseinfo/unit/attach/update?id=" + values[0] + "&father_id=" + params.father_id+"&fk_unit="+params.fk_unit;
				eventBusService.publish("modules.content.load",menu);
			}
		}
		if(event=='delete'){
			var values = [];
			var ids="";
			$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
				values.push($(this).val());
				ids = ids+",'"+$(this).val()+"'";
			});
			if(values.length < 1){
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择修改数据。"});
			}else{
				eventBusService.publish("modal.alert",{"content":"你是否确认删除这"+values.length+"数据？","isAutoClose":false,"confirm":function(){
						httpService.post(app.urls.unit_deleteAttach,{"id":ids.replace(',','')}, function(data) {
							if ("0000" === data.code) {
								$scope.find();
							} else {
								alert(data.msg);
							}
						});
					},"cancel":function(){
						//用户取消，不做操作
					}});
			}
		}
		if(event=='export'){
			var values = [];
			$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
				values.push($(this).val());
			});
			$scope.form.list = JSON.stringify(values);
			httpService.post(app.urls.attach_export,$scope.form, function(data) {
				if ("0000" === data.code) {
					window.location.href = data.data;
				} else {
					alert(data.msg);
				}
			});
		}
		if(event=='template'){
			httpService.post(app.urls.attach_template,{}, function(data) {
				if ("0000" === data.code) {
					window.location.href = data.data;
				} else {
					alert(data.msg);
				}
			});
		}
		if(event=='import'){
			eventBusService.publish("modal.page","modules/content/baseinfo/unit/attach/import");
		}
	}
	
	//接收数据刷新
	eventBusService.subscribe('modules.page.reload', function(event,data) {
		$scope.select();
	});
		//选择数据
		$scope.optData = function (element) {
			if($(element).find("input[name='dataPk']").is(":checked")){
				$(element).find("input[name='dataPk']").prop("checked",false);
				$(element).removeClass("opt_tr");
			}else{
				$(element).find("input[name='dataPk']").prop("checked",true);
				$(element).addClass("opt_tr");
			}
		}
		$scope.checkAll = function(target) {
			$("#" + $scope.controllerName + " input[name='dataPk']").each(function() {
				if ($(this).prop("checked")) {
					$(this).prop('checked', false);
					$(this).parents("tr").removeClass("opt_tr");
				} else {
					$(this).prop('checked', true);
					$(this).parents("tr").addClass("opt_tr");
				}
			})
		}
}}});